
package com.epfresh.supplier.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.epfresh.api.universalimageloader.core.ImageLoader;
import com.epfresh.api.utils.FormatUtil;
import com.epfresh.supplier.R;
import com.epfresh.supplier.entity.HomeBuyer;

import multitype.ItemViewBinder;
import multitype.OnRecyclerViewItemClickListener;

public class HomeBuyerItemViewBinder
        extends ItemViewBinder<HomeBuyer, HomeBuyerItemViewBinder.ViewHolder> implements View.OnClickListener {

    boolean isShowTitle = true;
    public interface OnBuyerListener{
        void onBuyer(View view, int position, HomeBuyer data);
    }

    public boolean isShowAgent = true;//代办进入店铺 不显示拼货按钮

    OnBuyerListener onBuyerListener;
    OnRecyclerViewItemClickListener onRecyclerViewItemClickListener;

    public boolean isShowTitle() {
        return isShowTitle;
    }

    public void setShowTitle(boolean showTitle) {
        isShowTitle = showTitle;
    }

    public OnBuyerListener getOnBuyerListener() {
        return onBuyerListener;
    }

    public void setOnBuyerListener(OnBuyerListener onBuyerListener) {
        this.onBuyerListener = onBuyerListener;
    }

    public OnRecyclerViewItemClickListener getOnRecyclerViewItemClickListener() {
        return onRecyclerViewItemClickListener;
    }

    public void setOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener onRecyclerViewItemClickListener) {
        this.onRecyclerViewItemClickListener = onRecyclerViewItemClickListener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(
            @NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.item_home_buyer_item, parent, false);
        return new ViewHolder(view);
    }


    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull HomeBuyer post) {

        holder.rl_base.setTag(R.id.item_key_position, getPosition(holder));
        holder.rl_base.setTag(R.id.item_key_status, post);
        holder.rl_base.setOnClickListener(this);
        holder.rl_product.setTag(R.id.item_key_position, getPosition(holder));
        holder.rl_product.setTag(R.id.item_key_status, post);
        holder.rl_product.setOnClickListener(this);

        holder.tv_product_new.setTag(R.id.item_key_position, getPosition(holder));
        holder.tv_product_new.setTag(R.id.item_key_status, post);
        holder.tv_product_new.setOnClickListener(this);


        holder.setData(post);
        if(post.isCanGroupon()){
            holder.tv_product_new.setVisibility(View.VISIBLE);
            holder.iv_product_new.setVisibility(View.VISIBLE);
            holder.tv_product_empty_agency.setVisibility(View.GONE);
        }else {
            holder.tv_product_new.setVisibility(View.GONE);
            holder.iv_product_new.setVisibility(View.GONE);
            holder.tv_product_empty_agency.setVisibility(View.VISIBLE);
        }
        if (isShowTitle) {
            holder.rl_base.setVisibility(View.VISIBLE);
        } else {
            holder.rl_base.setVisibility(View.GONE);
        }


    }

    @Override
    public void onClick(View v) {

        Object tag = v.getTag(R.id.item_key_position);
        if (tag != null && tag instanceof Integer) {
            int position = (int) tag;
            Object o = v.getTag(R.id.item_key_status);


            switch (v.getId()) {
                case R.id.tv_product_new:

                    if (onBuyerListener != null && o instanceof HomeBuyer){
                        onBuyerListener.onBuyer(v,position, (HomeBuyer) o);
                    }
                    break;

                default:
                    if (onRecyclerViewItemClickListener != null && o instanceof HomeBuyer) {
                        onRecyclerViewItemClickListener.onItemClick(v, position, o);
                    }
                    break;
            }


        }
    }


    static class ViewHolder extends RecyclerView.ViewHolder {
        private View rl_base;
        private View rl_product;

        private ImageView iv_product;
        private TextView tv_store_title, tv_product_title, tv_product_price, tv_product_package, tv_product_size, tv_product_new;
        private TextView tvTime;
        private TextView tvContent;

        private ImageView iv_product_new;

        private View tv_product_empty_agency;


        ViewHolder(@NonNull View itemView) {
            super(itemView);
            iv_product = itemView.findViewById(R.id.iv_product);
            rl_base = itemView.findViewById(R.id.rl_base);

            tv_store_title = itemView.findViewById(R.id.tv_store_title);
            tv_product_title = itemView.findViewById(R.id.tv_product_title);
            tv_product_price = itemView.findViewById(R.id.tv_product_price);
            tv_product_package = itemView.findViewById(R.id.tv_product_package);
            tv_product_size = itemView.findViewById(R.id.tv_product_size);
            tv_product_new = itemView.findViewById(R.id.tv_product_new);
            iv_product_new = itemView.findViewById(R.id.iv_product_new);
            rl_product = itemView.findViewById(R.id.rl_product);
            tv_product_empty_agency = itemView.findViewById(R.id.tv_product_empty_agency);
        }


        void setData(HomeBuyer post) {

//            tv_store_title.setText(post.getTitle());

            ImageLoader.getInstance().displayImage(post.getThumbnail(), iv_product);
            tv_store_title.setText(post.getStoreName());
            tv_product_title.setText(post.getTitle());
            String minPrice = post.getMinPrice();
            String maxPrice = post.getMaxPrice();
            String price;
            if (minPrice != null && !minPrice.equals(maxPrice)) {
                price = FormatUtil.subZeroAndDot(minPrice) + "元~" + FormatUtil.subZeroAndDot(maxPrice) + "元/" + post.getChargeUnit();
            } else if (minPrice == null) {
                price = "";
            } else {
                price = FormatUtil.subZeroAndDot(minPrice) + "元/" + post.getChargeUnit();
            }
            tv_product_price.setText(price);

            String pack = post.getPackageSize() + post.getPackageUnit() + "/" + post.getPackageContainer();
            tv_product_package.setText(pack);
            String pSize = "满"+FormatUtil.subZeroAndDot(post.getMinOrderQuantity()) + post.getChargeUnit() + "可发货，最高可拼" + FormatUtil.subZeroAndDot(post.getMaxOrderQuantity()) + post.getChargeUnit();
            tv_product_size.setText(pSize);



        }
    }
}
